package com.example.fdbus

import android.util.Log
import com.google.gson.Gson
import java.lang.reflect.InvocationHandler
import java.lang.reflect.Method

class MyNameServerInvocationHandler(val clazz: Class<*>) : InvocationHandler {
    private val TAG = MyNameServerInvocationHandler::class.java.name
    private val gson by lazy {
        Gson()
    }
    override fun invoke(proxy: Any?, method: Method?, args: Array<out Any>?): Any? {
        if (method?.name!!.contains("toString")) return "proxy"

        Log.i(TAG, "invoke: ${method!!.name}")
        //
        val result = IPCNameManager.sendServerRequest(clazz, method, args, NameServerManager.TYPE_INVOKE)

        return gson.fromJson(result, method.returnType)
    }

}